package com.wicket.okrframework.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrframework.base.service.MUsersService;
import com.wicket.okrframework.base.service.dto.req.BatchQueryUserReq;
import com.wicket.okrframework.base.service.dto.req.QueryUserDetailReq;
import com.wicket.okrframework.base.service.dto.req.QueryUserListReq;
import com.wicket.okrframework.common.annotations.BanAuto;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.common.util.CommonFunctionHelper;
import com.wicket.okrframework.dal.mapper.mbg.UsersMapper;
import com.wicket.okrframework.dal.po.mbg.UnionApp;
import com.wicket.okrframework.dal.po.mbg.Users;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.wicket.okrframework.base.service.dto.req.BatchDeleteUserTestDataReq;
import com.wicket.okrframework.base.service.dto.req.BatchQueryUserTestDataReq;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MUsersServiceImpl
        extends ServiceImpl<UsersMapper, Users>
        implements MUsersService {

    @Autowired
    UsersMapper usersMapper;

    /**
   * 1-2-01查询个人账号详情[286]
   * gen by moon at 10/16/2022, 11:06:08 AM
   */
 @Trace(operationName = "1-2-01查询个人账号详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  Users  queryUserDetail(QueryUserDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<Users> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                Users retData = retList.get(0);

                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }

    /**
   * 1-2-01修改个人账号[46]
   * gen by moon at 10/16/2022, 11:06:06 AM
   */
 @Trace(operationName = "1-2-01修改个人账号")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateUser(Users users) {
     users.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(users);
  }

    /**
   * 1-2-01新增个人账号[45]
   * gen by moon at 6/25/2023, 12:58:27 AM
   */
 @Trace(operationName = "1-2-01新增个人账号")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addUser(Users users) {
     users.setUserId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
users.setRegTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
users.setFailedPasswordAttemptCount(Long.valueOf(0));//CUSTOM_CONVENTION
users.setIsValid("FALSE");
users.setIsInvalid("FALSE");
users.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
users.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
     if(super.save(users)){
        return users.getUserId();
     }else {
        return "-1";
     }
    
  }

    /**
   * 1-2-01批量查个人账号[1293]
   * gen by moon at 9/14/2022, 4:09:56 PM
   */
 @Trace(operationName = "1-2-01批量查个人账号")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
 @BanAuto
  @Transactional(rollbackFor=Exception.class)
  public  List<Users>  batchQueryUser(BatchQueryUserReq reqDto) {
     
        try {
            QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
            if(CommonFunctionHelper.getBaseInfoFromLocal().getSpecEndTime()!=null && CommonFunctionHelper.getBaseInfoFromLocal().getSpecStartTime()!=null){
              queryWrapper.le("reg_time",CommonFunctionHelper.getBaseInfoFromLocal().getSpecEndTime())
                    .ge("reg_time",CommonFunctionHelper.getBaseInfoFromLocal().getSpecStartTime());//TIME_RANGE
             }
            if(reqDto.getUsersList()==null || reqDto.getUsersList().isEmpty()){
                    return new ArrayList<>();
                }
        queryWrapper.in("user_id", reqDto.getUsersList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Users.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Users.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Users.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("last_login_time");
            
            return ((List<Users>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 1-2-01查询个人账号列表[48]
   * gen by moon at 9/14/2022, 4:09:54 PM
   */
 @Trace(operationName = "1-2-01查询个人账号列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
 @BanAuto
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<Users>  queryUserList(QueryUserListReq reqDto) {
     
        try {
            QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
            if(CommonFunctionHelper.getBaseInfoFromLocal().getSpecStartTime()!=null && CommonFunctionHelper.getBaseInfoFromLocal().getSpecEndTime()!=null){
              queryWrapper.le("reg_time",CommonFunctionHelper.getBaseInfoFromLocal().getSpecEndTime())
                    .ge("reg_time",CommonFunctionHelper.getBaseInfoFromLocal().getSpecStartTime());//TIME_RANGE
             }
reqDto.setIsValid("FALSE");
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Users.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Users.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Users.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("last_login_time");
            queryWrapper.orderByDesc("create_time");
queryWrapper.orderByDesc("operate_time");
            return ((List<Users>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 1-2-01删除个人账号[47]
   * gen by moon at 10/16/2022, 11:06:07 AM
   */
 @Trace(operationName = "1-2-01删除个人账号")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  deleteFrameworkUsers(String userId ) {
     
      return super.removeById(userId);
  }
/**
   * M1批量删个人账号测试[4856]
   * gen by moon at 10/16/2022, 11:06:08 AM
   */
 @Trace(operationName = "M1批量删个人账号测试")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  batchDeleteUserTestData(List<String> reqDto) {
     
          if(reqDto==null || reqDto.size()==0){
            return false;
          }
          
          return super.removeByIds(reqDto);
  }
/**
   * M1批量查询个人账号测试[4885]
   * gen by moon at 10/16/2022, 11:06:09 AM
   */
 @Trace(operationName = "M1批量查询个人账号测试")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<Users>  batchQueryUserTestData(BatchQueryUserTestDataReq reqDto) {
     
        try {
            QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
            
            if(reqDto.getUserTestDataList()==null || reqDto.getUserTestDataList().isEmpty()){
                    return new ArrayList<>();
                }
        queryWrapper.in("user_id", reqDto.getUserTestDataList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Users.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Users.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Users.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<Users>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
/**
   * 1-2-01执行账号使用无效密码登录次数自动加一[8425]
   * gen by moon at 2/3/2024, 8:21:52 PM
   */
 @Trace(operationName = "1-2-01执行账号使用无效密码登录次数自动加一")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  implementUserFailedPasswordAttemptCountAtuoAddOne(Users users) {
     UpdateWrapper<Users> uw = new UpdateWrapper<>();
        uw.eq("user_id",users.getUserId());
        users.setFailedPasswordAttemptCount(1L);
        uw.setSql("failed_password_attempt_count = failed_password_attempt_count + " + users.getFailedPasswordAttemptCount());//ADD_NUMERICAL
        users.setFailedPasswordAttemptCount(null);
        return super.update(users,uw);

  }
}
